package com.cn.barcode;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.util.Log;

public class WebServiceManager {
	/* 服务 地址 */
	// public static final String SERVICE_URL =
	// "http://116.228.165.182:8800/mobilews/optService";
	public String service_url;
	/* socket 错误 */
	public static final int SOCKET_ERR = 1111;

	private static WebServiceManager wsm = null;

	public static WebServiceManager getInstance() {
		if (wsm != null) {
			return wsm;
		} else {
			return wsm = new WebServiceManager();
		}
	}

	/**
	 * @return the wsm
	 */
	public static WebServiceManager getWsm() {
		return wsm;
	}

	public static void setWsm(WebServiceManager wsm) {
		WebServiceManager.wsm = wsm;
	}

	/**
	 * @return the socketErr
	 */
	public static int getSocketErr() {
		return SOCKET_ERR;
	}

	public void setServiceurl(String url) {
		this.service_url = url;
	}

	public String getDataByPost(String url, String scanString, String numble) {
		if (url != null) {
			Log.v("WebServiceManager:getData:", url);
		}
		HttpClient httpClient = null;
		String result = null;
		try {
			HttpParams params = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(params, 10 * 1000);
			HttpConnectionParams.setSoTimeout(params, 10 * 1000);
			httpClient = new DefaultHttpClient(params);
			HttpPost httppost = new HttpPost(url);
			List<NameValuePair> list = new ArrayList<NameValuePair>();
			NameValuePair pair1 = new BasicNameValuePair("scanresult",
					scanString);
			NameValuePair pair2 = new BasicNameValuePair("numble", numble);
			list.add(pair1);
			list.add(pair2);
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,
					"UTF-8");
			// 设置请求内容
			httppost.setEntity(entity);
			// 发送请求
			HttpResponse response = httpClient.execute(httppost);
			// 取得回复码
			int code = response.getStatusLine().getStatusCode();
			Log.v("WebServiceManager:getData:code", code + "");
			InputStream is = response.getEntity().getContent();
			result = readString(is);
			is.close();
			return result;
		} catch (Exception e) {
			Log.v("WebServiceManager:getData:Exception", e.getMessage());
			return result + "WebServiceManager:getData:Exception";
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
	}

	public String getDataByGet(String url, String scanString, String numble) {
		if (url != null) {
			Log.v("WebServiceManager:getData:", url);
		}
		HttpParams params = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(params, 10 * 500);
		HttpConnectionParams.setSoTimeout(params, 10 * 500);

		HttpClient httpClient = new DefaultHttpClient(params);
		String result = null;
		StringBuilder buf = new StringBuilder(url);
		buf.append("?");
		try {
			buf.append("code=" + URLEncoder.encode(scanString, "UTF-8") + "&");
			buf.append("numble=" + URLEncoder.encode(numble, "UTF-8"));
			HttpGet get = new HttpGet(buf.toString());
			HttpResponse response = httpClient.execute(get);

			if (response.getStatusLine().getStatusCode() == 200) {
				InputStream in = response.getEntity().getContent();
				int code = response.getStatusLine().getStatusCode();
				Log.v("WebServiceManager:getData:code", code + "");
				result = readString(in);
			} else {
				result = "WebServiceManager:getData:code";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return result + "WebServiceManager:getData:Exception";
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return result;

	}

	protected String readString(InputStream in) throws Exception {
		byte[] data = new byte[1024];
		int length = 0;
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		while ((length = in.read(data)) != -1) {
			bout.write(data, 0, length);
		}
		return new String(bout.toByteArray(), "UTF-8");

	}

}
